/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.contract.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.cargo.entity.BizCargo;
import vip.xiaonuo.biz.modular.cargo.service.BizCargoService;
import vip.xiaonuo.biz.modular.contract.entity.vo.BizContractVO;
import vip.xiaonuo.biz.modular.customer.entity.BizCustomer;
import vip.xiaonuo.biz.modular.customer.service.BizCustomerService;
import vip.xiaonuo.biz.modular.warehousetype.entity.BizWarehouseType;
import vip.xiaonuo.biz.modular.warehousetype.service.BizWarehouseTypeService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.contract.entity.BizContract;
import vip.xiaonuo.biz.modular.contract.mapper.BizContractMapper;
import vip.xiaonuo.biz.modular.contract.param.BizContractAddParam;
import vip.xiaonuo.biz.modular.contract.param.BizContractEditParam;
import vip.xiaonuo.biz.modular.contract.param.BizContractIdParam;
import vip.xiaonuo.biz.modular.contract.param.BizContractPageParam;
import vip.xiaonuo.biz.modular.contract.service.BizContractService;
import vip.xiaonuo.common.util.BeanCopyUtils;

import java.util.List;

/**
 * 合同管理Service接口实现类
 *
 * @author lj
 * @date 2022/11/19 10:40
 **/
@Service
public class BizContractServiceImpl extends ServiceImpl<BizContractMapper, BizContract> implements BizContractService {

    @Autowired
    private BizWarehouseTypeService warehouseTypeService;

    @Autowired
    private BizCustomerService customerService;


    @Autowired
    private BizCargoService CargoService;

    @Override
    public Page<BizContract> page(BizContractPageParam bizContractPageParam) {
        QueryWrapper<BizContract> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizContractPageParam.getCustomerId())) {
            queryWrapper.lambda().eq(BizContract::getCustomerId, bizContractPageParam.getCustomerId());
        }
        if (ObjectUtil.isNotEmpty(bizContractPageParam.getStartContractStart()) && ObjectUtil.isNotEmpty(bizContractPageParam.getEndContractStart())) {
            queryWrapper.lambda().between(BizContract::getContractStart, bizContractPageParam.getStartContractStart(), bizContractPageParam.getEndContractStart());
        }
        if (ObjectUtil.isNotEmpty(bizContractPageParam.getStartContractEnd()) && ObjectUtil.isNotEmpty(bizContractPageParam.getEndContractEnd())) {
            queryWrapper.lambda().between(BizContract::getContractEnd, bizContractPageParam.getStartContractEnd(), bizContractPageParam.getEndContractEnd());
        }
        if (ObjectUtil.isNotEmpty(bizContractPageParam.getWarehouseTypeId())) {
            queryWrapper.lambda().eq(BizContract::getWarehouseTypeId, bizContractPageParam.getWarehouseTypeId());
        }
        if (ObjectUtil.isNotEmpty(bizContractPageParam.getCargoId())) {
            queryWrapper.lambda().eq(BizContract::getCargoId, bizContractPageParam.getCargoId());
        }
        if (ObjectUtil.isNotEmpty(bizContractPageParam.getType())) {
            queryWrapper.lambda().eq(BizContract::getType, bizContractPageParam.getType());
        }
        if (ObjectUtil.isAllNotEmpty(bizContractPageParam.getSortField(), bizContractPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizContractPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizContractPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizContractPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizContract::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public void add(BizContractAddParam bizContractAddParam) {
        BizContract bizContract = BeanUtil.toBean(bizContractAddParam, BizContract.class);
        this.save(bizContract);
    }

    @Override
    public void edit(BizContractEditParam bizContractEditParam) {
        BizContract bizContract = this.queryEntity(bizContractEditParam.getId());
        BeanUtil.copyProperties(bizContractEditParam, bizContract);
        this.updateById(bizContract);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizContractIdParam> bizContractIdParamList) {
        // 执行删除
        this.removeBatchByIds(CollStreamUtil.toList(bizContractIdParamList, BizContractIdParam::getId));
    }

    @Override
    public BizContractVO detail(BizContractIdParam bizContractIdParam) {
        BizContract contract = this.queryEntity(bizContractIdParam.getId());
        BizContractVO bizContractVO = BeanCopyUtils.copyBean(contract, BizContractVO.class);
        BizCustomer bizCustomer = customerService.queryEntity(contract.getCustomerId());
        BizWarehouseType bizWarehouseType = warehouseTypeService.queryEntity(contract.getWarehouseTypeId());
        BizCargo bizCargo = CargoService.queryEntity(contract.getCargoId());
        bizContractVO.setCustomer(bizCustomer);
        bizContractVO.setWarehouseType(bizWarehouseType);
        bizContractVO.setCargo(bizCargo);
        return bizContractVO;
    }

    @Override
    public BizContract queryEntity(String id) {
        BizContract contract = this.getById(id);
        if (ObjectUtil.isEmpty(contract)) {
            throw new CommonException("合同不存在，id值为：{}", id);
        }

        return contract;
    }
}
